// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CaptchaDialog.cs" company="3po.ru">
//   Author: Alexander Ulitin [support@3po.ru]
//   
//   This file is part of Conusic.
//   
//   Conusic is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//   
//   Conusic is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//   
//   You should have received a copy of the GNU General Public License
//   along with Conusic.  If not, see http://www.gnu.org/licenses/.
// </copyright>
// <summary>
//   The captcha dialog.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ConusicUI
{
    using System;
    using System.Drawing;
    using System.Windows.Forms;

    using Conusic;

    /// <summary>
    /// The captcha dialog.
    /// </summary>
    internal sealed class CaptchaDialog : Captcha
    {
        #region Fields

        /// <summary>
        /// The owner.
        /// </summary>
        private readonly Form owner = null;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CaptchaDialog"/> class.
        /// </summary>
        /// <param name="owner">
        /// The owner.
        /// </param>
        public CaptchaDialog(Form owner)
        {
            this.owner = owner;
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The request enter captcha.
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public override string RequestEnterCaptcha()
        {
            string url = string.Format(
                "{0}captcha.php?s=1&sid={1}", Conusic.Properties.Settings.Default.VkontakteAddress, this.sid);
            Image img = this.GetCaptcha(url);

            var form = new CaptchaForm(img);
            if (this.owner != null)
            {
                this.owner.Invoke((MethodInvoker)(() => form.ShowDialog(this.owner)));
            }
            else
            {
                form.ShowDialog();
            }

            string result = form.enteredText;
            form.Dispose();

            return result;
        }

        #endregion
    }
}